﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Server.Models;
using Server.Share;

namespace Server.Controllers.Manage
{
    [RoutePrefix("51API/Auth")]
    public class AuthController : ApiController
    {
        private MShare comm = new MShare();
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("GetAuthList")]
        public IHttpActionResult PostGetAuthList(string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id,"asd");
                AuthListModel[] au = comm.authCtrl.GetAuthCate();
                RetModel ret = new RetModel
                {
                    Data = au,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Auth001", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 获取用户对应权限
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("GetAuthorList")]
        public IHttpActionResult PostGetAuthorList(int id,string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                AuthorModel[] au = comm.authCtrl.GetAuthorList(id);
                RetModel ret = new RetModel
                {
                    Data = au,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Auth002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 保存管理员权限
        /// </summary>
        /// <param name="author">权限对应模型数组</param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("SaveAuthorInfo")]
        public IHttpActionResult PostSaveAuthorInfo(AuthorModel[] author,string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.authCtrl.SaveAuthorInfo(author);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Auth003", ErrInfo = ex.Message, Data = null });
            }
        }
    }
}